package com.manor.paradise.activity

import android.view.View
import com.gyf.immersionbar.ktx.immersionBar
import com.manor.common.activity.AbsActivity
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.*
import com.manor.common.utils.content
import com.manor.paradise.R
import com.manor.paradise.adapter.LybMiAdapter
import com.manor.paradise.adapter.LybMkAdapter
import com.manor.paradise.adapter.LybPowerAdapter
import com.manor.paradise.bean.*
import com.manor.paradise.http.ElvesHttpUtil
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.lyb_activity_mi_and_power.*

class LybMiAndPowerActivity : AbsActivity() {
    private var mPosition = 0
    private var pageNum = 1

    private val mMlList = arrayListOf<OresBean.RecordsBean>()
    private val mMlAdapter by lazy { LybMiAdapter(mMlList) }
    private val mMkList = arrayListOf<RecordSBean>()
    private val mMkAdapter by lazy { LybMkAdapter(mMkList) }
    private val mPowerList = arrayListOf<CrystalBean.RecordsBean>()
    private val mPowerAdapter by lazy { LybPowerAdapter(mPowerList) }
    private var paradiseInfoBean: ParadiseInfoBean? = null
    override fun getLayoutId() = R.layout.lyb_activity_mi_and_power
    override fun main() {
        super.main()
        immersionBar {
            statusBarColor(R.color.white)
            titleBar(mTitleBarLayout)
            statusBarDarkFont(true)
        }
        setTopTitle("米粒/能量", rightIv = R.drawable.question_mark)
        mDetailsRv.adapter = mMlAdapter
        initMode()
        setData()
        setClick(mMlBtn, mMkBtn, mPowerBtn)
        selected(mMlBtn, mMkBtn, mPowerBtn)
        mDetailsSrl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData()
            }
        })
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when(v?.id){
            R.id.mRightIv ->{
                LybActiveExplainActivity.launch(1)
            }
            R.id.mMlBtn ->{
                showWaitingDialog()
                mPosition = 0
                selected(mMlBtn, mMkBtn, mPowerBtn)
                paradiseInfoBean?.run {
                    mMlAndMkTitleTv.content("米粒")
                    mDetailsTitleTv.content("米粒明细")
                    mPowerLayout.v(false)
                    mMlLayout.v(true)
                    mPropertyTv.content(crystalOre)
                    mTodayProductionValueTv.content(todayCrystalOre)
                    mFreezeValueTv.content(frozenCrystalOre)
                    pageNum = 1
                    setData()
                }
            }
            R.id.mMkBtn ->{
                showWaitingDialog()
                mPosition = 1
                selected(mMkBtn, mMlBtn, mPowerBtn)
                paradiseInfoBean?.run {
                    mMlAndMkTitleTv.content("米壳总数")
                    mDetailsTitleTv.content("米壳明细")
                    mPowerLayout.v(false)
                    mMlLayout.v(false)
                    mPropertyTv.content(startPoint.scaleDown(4))
                    pageNum = 1
                    setData()
                }
            }
            R.id.mPowerBtn ->{
                showWaitingDialog()
                mPosition = 2
                selected(mPowerBtn, mMkBtn, mMlBtn)
                paradiseInfoBean?.run {
                    mMlAndMkTitleTv.content("能量")
                    mDetailsTitleTv.content("能量明细")

                    mPowerLayout.v(true)
                    mMlLayout.v(false)
                    mPropertyTv.content(crystal.scaleDown(4))
                    mDailyProductionTv.content(todayCrystal)
                    pageNum = 1
                    setData()
                }
            }
        }
    }

    private fun initMode() {
        ElvesHttpUtil.getParadiseInfo(object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                paradiseInfoBean = GsonUtil.gsonToBean(info, ParadiseInfoBean::class.java)
                paradiseInfoBean?.run {
                    mPropertyTv.content(crystalOre)
                    mTodayProductionValueTv.content(todayCrystalOre)
                    mFreezeValueTv.content(frozenCrystalOre)
                }
            }
        })
    }

    fun setData() {
        when (mPosition) {
            0 -> {
                if (pageNum == 1) {
                    mDetailsRv.adapter = mMlAdapter
                }

                ElvesHttpUtil.getOresList(pageNum, object : JHttpCallback() {
                    override fun onSuccess(code: Int, msg: String, info: String) {
                        dismissWaitingDialog()
                        if (code != 200) {
                            ToastUtil.show(msg)
                            return
                        }
                        var oresBean: OresBean = GsonUtil.gsonToBean<OresBean>(info, OresBean::class.java)
                        KLog.e("json", oresBean);
                        if (pageNum == 1) {
                            mMlAdapter.setNewInstance(oresBean.records)
                            mDetailsSrl.finishRefresh()
                        } else {
                            mMlAdapter.addData(oresBean.records)
                            mDetailsSrl.finishLoadMore()
                        }
                        if (mMlAdapter.data.isEmpty()) {
                            mMlAdapter.setEmptyView(R.layout.view_empty_default)
                        }
                    }

                    override fun onError() {
                        super.onError()
                        dismissWaitingDialog()
                        mDetailsSrl.finishRefresh()
                        mDetailsSrl.finishLoadMore()
                    }
                })
            }
            1 -> {
                if (pageNum == 1) {
                    mDetailsRv.adapter = mMkAdapter
                }

                ElvesHttpUtil.getStartPointList(pageNum, object : JHttpCallback() {
                    override fun onSuccess(code: Int, msg: String, info: String) {
                        dismissWaitingDialog()
                        if (code != 200) {
                            ToastUtil.show(msg)
                            return
                        }
                        var oresBean: StartPointBean = GsonUtil.gsonToBean<StartPointBean>(info, StartPointBean::class.java)
                        KLog.e("json", oresBean);
                        if (pageNum == 1) {
                            mMkAdapter.setNewInstance(oresBean.records)
                            mDetailsSrl.finishRefresh()
                        } else {
                            mMkAdapter.addData(oresBean.records)
                            mDetailsSrl.finishLoadMore()
                        }
                        if (mMkAdapter.data.isEmpty()) {
                            mMkAdapter.setEmptyView(R.layout.view_empty_default)
                        }
                    }

                    override fun onError() {
                        super.onError()
                        dismissWaitingDialog()
                        mDetailsSrl.finishRefresh()
                        mDetailsSrl.finishLoadMore()
                    }
                })
            }
            2 -> {
                if (pageNum == 1) {
                    mDetailsRv.adapter = mPowerAdapter
                }
                ElvesHttpUtil.getCrystalList(pageNum, object : JHttpCallback() {
                    override fun onSuccess(code: Int, msg: String, info: String) {
                        dismissWaitingDialog()
                        if (code != 200) {
                            ToastUtil.show(msg)
                            return
                        }
                        var crystalBean: CrystalBean = GsonUtil.gsonToBean<CrystalBean>(info, CrystalBean::class.java)
                        KLog.e("json", crystalBean);
                        if (pageNum == 1) {
                            mPowerAdapter.setNewInstance(crystalBean.records)
                            mDetailsSrl.finishRefresh()
                        } else {
                            mPowerAdapter.addData(crystalBean.records)
                            mDetailsSrl.finishLoadMore()
                        }
                        if (mPowerAdapter.data.isEmpty()) {
                            mPowerAdapter.setEmptyView(R.layout.view_empty_default)
                        }
                    }

                    override fun onError() {
                        super.onError()
                        dismissWaitingDialog()
                        mDetailsSrl.finishRefresh()
                        mDetailsSrl.finishLoadMore()
                    }
                })
            }
        }
    }
}